import { expect } from 'chai';
import mergeUrl from '../src/mergeUrl';

describe('mergeUrl unit test', function() {
    it('should return empty if missing arguments', function() {
        expect(mergeUrl()).to.be.equal('');
    });

    it('should return url if search is empty', function() {
        var url = 'www.example.com';
        expect(mergeUrl(url)).to.be.equal(url);
    });

    it('should return right url', function() {
        var url = 'http://example.com';
        var search = '?foo=bar';
        expect(mergeUrl(url, search)).to.be.equal(url + search);
    });

    it('should return right url', function() {
        var url = 'http://example.com';
        var search = 'foo=bar';
        expect(mergeUrl(url, search)).to.be.equal(url + '?' + search);
    });

    it('should return right url if url has search', function() {
        var url = 'http://example.com?hello=world';
        var search = 'foo=bar';
        expect(mergeUrl(url, search)).to.be.equal(url + '&' + search);
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?foo=bar';
        expect(mergeUrl(url, search)).to.be.equal(url + '&foo=bar');
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?hello=bar&foo=bar';
        expect(mergeUrl(url, search, ['hello'])).to.be.equal(url + '&foo=bar');
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?hello=bar&foo=bar&hello=baz';
        expect(mergeUrl(url, search, ['hello'])).to.be.equal(url + '&foo=bar');
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?foo=bar&hello=baz&age=42';
        expect(mergeUrl(url, search, ['hello'])).to.be.equal(url + '&foo=bar&age=42');
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?foo=bar&hello=baz&age=42';
        expect(mergeUrl(url, search, ['hello', 'age'])).to.be.equal(url + '&foo=bar');
    });

    it('should return right url if url has search and search has ?', function() {
        var url = 'http://example.com?hello=world';
        var search = '?foo=bar&hello=baz&age=42';
        expect(mergeUrl(url, search)).to.be.equal(url + '&foo=bar&hello=baz&age=42');
    });

    it('should return right url if url has the same search as provided params', function() {
        var url = 'http://example.com?hello=world';
        var search = '?hello=baz';
        expect(mergeUrl(url, search, ['hello'])).to.be.equal(url);
    });
});